package cn.ecnu.utils;

import cn.ecnu.entity.Address;
import cn.ecnu.feign.ManagementFeignService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

@Service
public class RouteGenerator {

    @Resource
    private ManagementFeignService managementFeignService;

    @Resource
    private ObjectMapper objectMapper;

    private static final String[] CITIES = new String[]{"上海市", "北京市", "绵阳市", "信阳市", "南京市",
            "苏州市", "杭州市", "九江市", "呼和浩特市", "合肥市", "深圳市", "广州市",
            "三亚市", "厦门市", "哈尔滨市"};

    public String generate(Integer sendAddressId, Integer receiveAddressId) {
        String sendAddress = objectMapper.convertValue(managementFeignService.getAddressById(sendAddressId).get("data"), Address.class).getAddress();
        String sendCity = sendAddress.substring(0, sendAddress.indexOf('市') + 1);
        String receiveAddress = objectMapper.convertValue(managementFeignService.getAddressById(receiveAddressId).get("data"), Address.class).getAddress();
        String receiveCity = receiveAddress.substring(0, receiveAddress.indexOf('市') + 1);
        StringBuilder builder = new StringBuilder();
        builder.append(sendCity).append("|");
        //随机生成两个途经城市
        for (int i = 0; i < 1; i++) {
            int index = new Random().nextInt(CITIES.length);
            builder.append(CITIES[index]).append("|");
        }
        builder.append(receiveCity);
        return builder.toString();
    }


}
